package com.fuwu8858.redismybatis.Dao;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class RedisDao {
	@Autowired
	private RedisTemplate stringRedisTemplate;
	
	
	public Set keys(String pattern){
		
		return stringRedisTemplate.keys(pattern);
	}
	public String get(String key){
		if(key!=null){
			return stringRedisTemplate.opsForValue().get(key).toString();
		}else{
			return "key为空"+key;
		}
		

		
	}
	/**
	 * 将value对象写入缓存
	 * @param key
	 * @param value
	 * @param time 失效时间(秒)
	 */
	public  void set(String key,Object value,Long time){
		if(value.getClass().equals(String.class)){
			stringRedisTemplate.opsForValue().set(key, value.toString());				
		}else if(value.getClass().equals(Integer.class)){
			stringRedisTemplate.opsForValue().set(key, value.toString());
		}else if(value.getClass().equals(Double.class)){
			stringRedisTemplate.opsForValue().set(key, value.toString());
		}else if(value.getClass().equals(Float.class)){
			stringRedisTemplate.opsForValue().set(key, value.toString());
		}else if(value.getClass().equals(Short.class)){
			stringRedisTemplate.opsForValue().set(key, value.toString());
		}else if(value.getClass().equals(Long.class)){
			stringRedisTemplate.opsForValue().set(key, value.toString());
		}else if(value.getClass().equals(Boolean.class)){
			stringRedisTemplate.opsForValue().set(key, value.toString());
		}else{
			stringRedisTemplate.opsForValue().set(key, value);			
		}
		if(time > 0){
			stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
		}
	}
	
	
}
